//
//  HomeTabBarController.swift
//  MyWeiBo
//
//  Created by 梅毅 on 2018/3/15.
//  Copyright © 2018年 Messi. All rights reserved.
//

import UIKit

class HomeTabBarController: UITabBarController {

    //var controllers:[ NSDictionary]?
    var nameSpace:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        guard let name = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else
        {
            return
        }
        nameSpace = name
        tabBar.tintColor = UIColor.darkGray
        
        initViewControllers()
    }

    func initViewControllers() -> Void {
        var controllers:[ NSDictionary]?
        
        let filePath = Bundle.main.path(forResource: "Tab.plist", ofType: nil);
        controllers = (NSArray.init(contentsOfFile: filePath!) as! [NSDictionary]);
        
        for (_,dict) in controllers!.enumerated() {
            addViewController(classInfo: dict)
        }
    }
    private func addViewController(classInfo:NSDictionary) -> Void {
        //动态获取命名空间
        guard let name = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else
        {
            return
        }
        var cls:AnyClass? = nil
        if let controllerName = (classInfo["className"] as? String)
        {
            cls = NSClassFromString(name + "." + controllerName)
        }
        //通过类创建对象
        guard let typeClass = cls as? UITableViewController.Type else
        {
            return
        }
        let childController = typeClass.init()
        childController.title = classInfo["title"] as? String
        if let imageName = (classInfo["imageName"] as? String)
        {
            childController.tabBarItem.image = UIImage.init(named: imageName)
        }
        if let selectIamgeName = (classInfo["selectIamge"] as? String)
        {
            childController.tabBarItem.selectedImage = UIImage.init(named: selectIamgeName)
        }
        let nav = UINavigationController.init(rootViewController: childController)
        
        addChildViewController(nav)
    }

}
